<?php

// 品牌街

namespace app\shop\controller;

use think\Request;
use think\Controller;
use app\shop\common\GuessGoods;
use app\shop\model\Ad as AdModel;
use app\shop\model\Brand as BrandModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\ShopGoods as ShopGoodsModel;

class Brand extends Controller
{
    /**
     * 品牌街主页
     * @param Request $request
     * @param AdModel $ad
     * @param BrandModel $brand
     * @param GoodsTypeModel $goodsType
     * @return mixed
     */
    public function index(Request $request, AdModel $ad, BrandModel $brand, GoodsTypeModel $goodsType)
    {
        // 字母筛选
        $letter = $request->param('letter', 'A');

        // 品牌街广告
        $adventure = $ad->where(['parenTid' => 9])->field('linkUrl,picUrl,skip_type,title')->order('id', 'desc')->limit(2)->select()->toArray();

        //查找推荐品牌
        $brand_type = $brand->where('recommend', 1)->order(['rand()' => 'desc'])->limit(7)->select()->toArray();

        // 按字母查询品牌
        $brands = $brand->where(['letter' => $letter])->order(['sort' => 'desc', 'update_time' => 'desc'])->paginate(36, false)->toArray();

        // // 获取商品分类信息
        // $type_arr = $goodsType->where(['status' => 1])->field('id,parenTid,title')->select();
        // $menu = get_cate($type_arr);

        return $this->fetch('', [
            'mate_head'  => '品牌街',
            'brands'     => $brands,
            'brand_type' => $brand_type,
            'ad'         => $adventure,
            'letter'     => letter()
        ]);

    }


    /**
     * 品牌下的商品列表
     * @param Request $request
     * @param ShopGoodsModel $shopGoods
     * @return mixed
     */
    public function goods(Request $request, ShopGoodsModel $shopGoods)
    {
        // 获取参数
        $inquire = $request->param();

        // 品牌筛选
        $condition['state'] = ['eq',1];
        $condition['status'] = ['eq',1];
        $condition['brand_id'] = $inquire['brand_id'];

        // 排序条件
        $sort = ['sort' => 'asc'];
        if (!empty($inquire['orderId'])) $sort = [$inquire['orderId'] => 'desc'];

        $data = $shopGoods->where($condition)->field('id,thumb_img,title,price,sale_number,inventory,hits,create_time,evaluate_number,member_id')->order($sort)->paginate(28, false, ['query' => $inquire]);

        // 商品总数
        $total = $shopGoods->where($condition)->count();

        // 猜你喜欢
        $guess_like = (new GuessGoods)->guessLike();

        return $this->fetch('', [
            'mate_head' => '商品列表',
            'total'     => $total,
            'data'      => $data,
            'guess_like' => $guess_like,
        ]);
    }
}